package cn.chenxi.hadoopproject.yearTemperature.service.impl;

import cn.chenxi.hadoopproject.yearTemperature.mapper.YearTemperatureMapper;
import cn.chenxi.hadoopproject.yearTemperature.pojo.dto.YearTemperatureQuery;
import cn.chenxi.hadoopproject.yearTemperature.pojo.entity.YearTemperature;
import cn.chenxi.hadoopproject.yearTemperature.pojo.vo.YearTemperatureVO;
import cn.chenxi.hadoopproject.yearTemperature.service.YearTemperatureService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class YearTemperatureServiceImpl implements YearTemperatureService {
    @Autowired
    private YearTemperatureMapper yearTemperatureMapper;

    @Override
    public List<YearTemperatureVO> select(YearTemperatureQuery yearTemperatureQuery) {
        log.debug("查询年平均气温，查询条件：{}", yearTemperatureQuery.getCityName());

        List<YearTemperature> yearTemperatures = yearTemperatureMapper.select(yearTemperatureQuery);
        List<YearTemperatureVO> yearTemperatureVOS = new ArrayList<>();

        // 创建一个 DecimalFormat 实例用于格式化浮点数
        DecimalFormat df = new DecimalFormat("#.00");

        for (YearTemperature yearTemperature : yearTemperatures) {
            String year = yearTemperature.getYearCity().split("_")[0];
            float maxTemp = Float.valueOf(df.format(yearTemperature.getMaxTemp()));
            float minTemp = Float.valueOf(df.format(yearTemperature.getMinTemp()));

            YearTemperatureVO yearTemperatureVO = new YearTemperatureVO();
            yearTemperatureVO.setYear(year);
            yearTemperatureVO.setMaxTemp(maxTemp);
            yearTemperatureVO.setMinTemp(minTemp);
            yearTemperatureVOS.add(yearTemperatureVO);
        }

        return yearTemperatureVOS;
    }
}
